﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nhom04_DiaDiemDuLich.DTO;
using System.Data.SqlClient;
using System.Data;

namespace Nhom04_DiaDiemDuLich.DAO
{
    public class KhachHangDAO : DataProvider
    {
        protected override object GetDataFromDataRow(System.Data.DataTable dt, int i)
        {
            KhachHangDTO result = new KhachHangDTO();

            result.MaKH = Int32.Parse(dt.Rows[i]["MaKH"].ToString());
            result.MatKhau = dt.Rows[i]["MatKhau"].ToString();
            result.Email = dt.Rows[i]["Email"].ToString();
            result.SoNha = dt.Rows[i]["SoNha"].ToString();
            result.Duong = dt.Rows[i]["Duong"].ToString();
            result.ThanhPho = dt.Rows[i]["ThanhPho"].ToString();
            result.DienThoai = dt.Rows[i]["DienThoai"].ToString();
            result.Passport = dt.Rows[i]["Passport"].ToString();
            result.Cmnd = dt.Rows[i]["Cmnd"].ToString();
            result.TenKH = dt.Rows[i]["TenKH"].ToString();
            result.Username = dt.Rows[i]["UserName"].ToString();
            return result;
        }

        public List<KhachHangDTO> getDSKHDatPhong(string tenks)
        {
            List<KhachHangDTO> result = new List<KhachHangDTO>();

            connect();

            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            command = new SqlCommand();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "sp_DSKHDatPhong";

            paramater = new SqlParameter("TenKS", SqlDbType.NVarChar);
            paramater.Direction = ParameterDirection.Input;
            paramater.Value = tenks;
            command.Parameters.Add(paramater);

            da.SelectCommand = command;
            da.Fill(ds);

            DataTable dt = ds.Tables[0];
            int n;
            if ((n = dt.Rows.Count) > 0)
            {
                for (int i = 0; i < n; i++)
                {
                    KhachHangDTO kh = (KhachHangDTO)GetDataFromDataRow(dt, i);
                    result.Add(kh);
                }
            }
            disconnect();

            return result;
        }

        public  KhachHangDTO KiemTraTaiKhoan(string UserName, string Password)
        {
            List<KhachHangDTO> result = new List<KhachHangDTO>();

            connect();

            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            command = new SqlCommand();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "sp_KiemTraTaiKhoanKhachHang";

            paramater = new SqlParameter("Username", SqlDbType.NVarChar);
            paramater.Direction = ParameterDirection.Input;
            paramater.Value = UserName;
            command.Parameters.Add(paramater);

            paramater = new SqlParameter("Password", SqlDbType.NVarChar);
            paramater.Direction = ParameterDirection.Input;
            paramater.Value = Password;
            command.Parameters.Add(paramater);

            da.SelectCommand = command;
            da.Fill(ds);

            DataTable dt = ds.Tables[0];
            int n;
            if ((n = dt.Rows.Count) > 0)
            {
                for (int i = 0; i < n; i++)
                {
                    KhachHangDTO kh = (KhachHangDTO)GetDataFromDataRow(dt, i);
                    result.Add(kh);
                }
            }
            disconnect();
            if (result == null || result.Count > 1)
            {
                return null;
            }
            else
                return result[0];

            return null;
        }
    }
}
